home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / GNU Diff Sources / GNU DIFF 1.15b Sources / diff3.h < prev    next >
Encoding:
Text File  |  1992-04-13  |  7.9 KB  |  266 lines  |  [TEXT/ALFA]

  1. /* Three-way file comparison program (diff3) for Project GNU
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /* Written by Randy Smith */
  20.  
  21. #undef    EXTERN
  22. #ifndef GDIFF3_MAIN
  23. #define EXTERN extern
  24. #else
  25. #define EXTERN
  26. #endif
  27.  
  28. /*
  29.  * Internal data structures and macros for the diff3 program; includes
  30.  * data structures for both diff3 diffs and normal diffs.
  31.  */
  32.  
  33. /*
  34.  * Different files within a diff
  35.  */
  36. #define    FILE0    0
  37. #define    FILE1    1
  38. #define    FILE2    2
  39.  
  40. /*
  41.  * Three way diffs are build out of two two-way diffs; the file which
  42.  * the two two-way diffs share is:
  43.  */
  44. #define    FILEC    FILE0
  45.  
  46. /* The ranges are indexed by */
  47. #define    START    0
  48. #define    END    1
  49.  
  50. enum diff_type {
  51.   ERROR,            /* Should not be used */
  52.   ADD,                /* Two way diff add */
  53.   CHANGE,            /* Two way diff change */
  54.   DELETE,            /* Two way diff delete */
  55.   DIFF_ALL,            /* All three are different */
  56.   DIFF_1ST,            /* Only the first is different */
  57.   DIFF_2ND,            /* Only the second */
  58.   DIFF_3RD            /* Only the third */
  59. };
  60.  
  61. #ifdef    DIRECT_DIFF
  62. /* Two-way diff */
  63. /*
  64.  * When "diff" is included with this project, the "diff_block"
  65.  * structure is exactly the same as a "change".
  66.  */
  67. #define    diff_block    change
  68. #define    next    link
  69.  
  70. #else    DIRECT_DIFF
  71. /* Two-way diff */
  72. struct diff_block {
  73.     LONG ranges[2][2];            /* Ranges are inclusive */
  74.     char **lines[2];        /* The actual lines (may contain nulls) */
  75.     LONG *lengths[2];        /* Line lengths (including newlines, if any) */
  76.     struct diff_block *link;
  77. };
  78. #endif
  79.  
  80. /* Three-way diff.
  81.  * Note that when the "diff" is included in this project, (struct diff3_block *)
  82.  * can be typecasted to (struct change *) safely!
  83.  * If you change this structure, also make sure you change "struct change".
  84.  */
  85.  
  86. struct diff3_block {
  87.     struct diff3_block *link;
  88.     char correspond;    /* Type of diff.  Really, the type is enum diff_type. */
  89.     LONG ranges[3][2];            /* Ranges are inclusive */
  90. #ifndef    DIRECT_DIFF
  91.   char **lines[3];        /* The actual lines (may contain nulls) */
  92.     LONG *lengths[3];        /* Line lengths (including newlines, if any) */
  93. #endif
  94. };
  95.  
  96. /*
  97.  * Access the ranges on a diff block.
  98.  */
  99. #define    D_LOWLINE(diff, filenum)    \
  100.   ((diff)->ranges[filenum][START])
  101. #define    D_HIGHLINE(diff, filenum)    \
  102.   ((diff)->ranges[filenum][END])
  103. #define    D_NUMLINES(diff, filenum)    \
  104.   (D_HIGHLINE((diff), (filenum)) - D_LOWLINE((diff), (filenum)) + 1)
  105.  
  106. /*
  107.  * Access the line numbers in a file in a diff by relative line
  108.  * numbers (i.e. line number within the diff itself).  Note that these
  109.  * are lvalues and can be used for assignment.
  110.  *
  111.  * However, when "diff" is included in the project, these
  112.  * are NOT lvalues!  This is because changing them would be
  113.  * changing the actual file buffers!
  114.  */
  115. #ifndef    DIRECT_DIFF
  116.  
  117. #define    D_RELNUM(diff, filenum, linenum)    \
  118.   (*((diff)->lines[filenum] + linenum))
  119. #define    D_RELLEN(diff, filenum, linenum)    \
  120.   (*((diff)->lengths[filenum] + linenum))
  121.  
  122.  #else    DIRECT_DIFF
  123.  
  124. #define    D_RELNUM(diff, filenum, linenum)    \
  125.     (diff3_file_data[filenum].linbuf[(linenum) + D_LOWLINE((diff), (filenum))].text + 0)
  126. #define    D_RELLEN(diff, filenum, linenum)    \
  127.     (diff3_file_data[filenum].linbuf[(linenum) + D_LOWLINE((diff), (filenum))].length + 0)
  128.  
  129. #endif    DIRECT_DIFF
  130.  
  131. /*
  132.  * And get at them directly, when that should be necessary.
  133.  */
  134. #define    D_LINEARRAY(diff, filenum)    \
  135.   ((diff)->lines[filenum])
  136. #define    D_LENARRAY(diff, filenum)    \
  137.   ((diff)->lengths[filenum])
  138.  
  139. /*
  140.  * Next block.
  141.  */
  142. #define    D_NEXT(diff)    ((diff)->next)
  143.  
  144. /*
  145.  * Access the type of a diff3 block.
  146.  */
  147. #define    D3_TYPE(diff)    ((diff)->correspond)
  148.  
  149. /*
  150.  * Line mappings based on diffs.  The first maps off the top of the
  151.  * diff, the second off of the bottom.
  152.  */
  153. #define    D_HIGH_MAPLINE(diff, fromfile, tofile, lineno)    \
  154.   ((lineno)                        \
  155.    - D_HIGHLINE ((diff), (fromfile))            \
  156.    + D_HIGHLINE ((diff), (tofile)))
  157.  
  158. #define    D_LOW_MAPLINE(diff, fromfile, tofile, lineno)    \
  159.   ((lineno)                        \
  160.    - D_LOWLINE ((diff), (fromfile))            \
  161.    + D_LOWLINE ((diff), (tofile)))
  162.  
  163. /*
  164.  * General memory allocation function.
  165.  */
  166. #define    ALLOCATE(number, type)    \
  167.   (type *) xmalloc ((number) * sizeof (type))
  168.  
  169. /*
  170.  * Options variables for flags set on command line.
  171.  *
  172.  * ALWAYS_TEXT: Treat all files as text files; never treat as binary.
  173.  *
  174.  * EDSCRIPT: Write out an ed script instead of the standard diff3 format.
  175.  *
  176.  * FLAGGING: Indicates that in the case of overlapping diffs (type
  177.  * DIFF_ALL), the lines which would normally be deleted from file 1
  178.  * should be preserved with a special flagging mechanism.
  179.  *
  180.  * DONT_WRITE_OVERLAP: 1 if information for overlapping diffs should
  181.  * not be output.
  182.  *
  183.  * DONT_WRITE_SIMPLE: 1 if information for non-overlapping diffs
  184.  * should not be output. 
  185.  *
  186.  * FINALWRITE: 1 if a :wq should be included at the end of the script
  187.  * to write out the file being edited.
  188.  *
  189.  * MERGE: output a merged file.
  190.  */
  191. EXTERN    int always_text;
  192. EXTERN    int edscript;
  193. EXTERN    int flagging;
  194. EXTERN    int dont_write_overlap;
  195. EXTERN    int dont_write_simple;
  196. EXTERN    int finalwrite;
  197. EXTERN    int merge;
  198.  
  199. extern int optind;
  200.  
  201. EXTERN    char *argv0;
  202.  
  203. #ifdef    DIRECT_DIFF
  204. /*
  205.  * We need this so we can access the buffers for the original files. */
  206. EXTERN    struct    file_data    diff3_file_data[3];
  207. #endif
  208.  
  209. /*
  210.  * Forward function declarations. (prototypes)
  211.  */
  212. #ifdef    __STDC__
  213. struct diff_block *process_diff (struct file_data *filea, struct file_data *fileb);
  214. struct diff3_block *make_3way_diff (struct diff_block *thread1, struct diff_block *thread2);
  215. void output_diff3 ( FILE *outputfile, struct diff3_block *diff, int mapping[3], int rev_mapping[3] );
  216. int output_diff3_edscript ( FILE *outputfile, struct diff3_block *diff, int mapping[3], int rev_mapping[3],  char *file0,  char *file1,  char *file2);
  217. int output_diff3_merge ();
  218. void usage (void);
  219.  
  220. struct diff3_block *using_to_diff3_block (struct diff_block *using[2], struct diff_block *last_using[2], int low_thread, int high_thread, struct diff3_block *last_diff);
  221. int copy_stringlist (char *fromptrs[], LONG *fromlengths, char *toptrs[], LONG *tolengths, LONG copynum);
  222. struct diff3_block *create_diff3_block (LONG low0, LONG high0, LONG low1, LONG high1, LONG low2, LONG high2);
  223. #ifndef    DIRECT_DIFF
  224. int compare_line_list ( char *list1[], LONG *lengths1,  char *list2[], LONG *lengths2, LONG nl);
  225. #else
  226. int compare_line_list ( struct line_def list1[], struct line_def list2[], LONG nl);
  227. #endif    DIRECT_DIFF
  228.  
  229. char *read_diff (char *filea, char *fileb, char **output_placement);
  230. enum diff_type process_diff_control ( char **string, struct diff_block *db);
  231. char *scan_diff_line ( char *scan_ptr,  char **set_start,  LONG *set_length,  char *limit, char firstchar);
  232.  
  233. struct diff3_block *reverse_diff3_blocklist (struct diff3_block *diff);
  234.  
  235. LONG myread (int fd, char *ptr, LONG size);
  236. int perror_with_exit (char *string);
  237.  
  238. int output_diff3_merge (FILE *commonfile, FILE *outputfile, struct diff3_block *diff, int mapping[3], int rev_mapping[3],
  239.             char *file0, char *file1, char *file2);
  240. #else    __STDC__
  241. /*
  242.  * Forward function declarations.
  243.  */
  244. struct diff_block *process_diff ();
  245. struct diff3_block *make_3way_diff ();
  246. void output_diff3 ();
  247. int output_diff3_edscript ();
  248. int output_diff3_merge ();
  249. void usage ();
  250.  
  251. struct diff3_block *using_to_diff3_block ();
  252. int copy_stringlist ();
  253. struct diff3_block *create_diff3_block ();
  254. int compare_line_list ();
  255.  
  256. char *read_diff ();
  257. enum diff_type process_diff_control ();
  258. char *scan_diff_line ();
  259.  
  260. struct diff3_block *reverse_diff3_blocklist ();
  261.  
  262. VOID *xmalloc ();
  263. VOID *xrealloc ();
  264.  
  265. #endif
  266.